#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parm_refresh.sh
##  Functions : 刷新参数
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-11-11  薛周毅           1.根据当前跑批日期刷新出后续所有hive过程中用到的参数
###############################################################################
:<<!
   错误码： 1 输入参数缺失
            2 输入日期参数不合法
!

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo "[ERROR]How to use this shell script!"
    echo "[ERROR]${V_SHELL_HOME}/edw_parm_refresh.sh 20170101 D"
}

if [[ -z ${1} || -z ${2} ]];then
    USAGE
    exit 1
else
    V_DATA_DATE=${1}
    V_FREQUENCY=${2}
fi

RE_CODE=$(IF_DATE ${V_DATA_DATE})
if [[ ${RE_CODE} -ne 0 ]]; then
    LOGGER "ERROR" "日期\"${V_DATA_DATE}\"格式不合法！"
    exit 2
fi

# 创建日志文件
if [[ ! -d "${V_SHELL_LOGS}/${V_DATA_DATE}" ]];then
    mkdir -p "${V_SHELL_LOGS}/${V_DATA_DATE}"
fi
# 创建数据导出目录
if [[ ! -d "${V_SHELL_DATA}/${V_DATA_DATE}" ]];then
    mkdir -p "${V_SHELL_DATA}/${V_DATA_DATE}"
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output



BEGIN
LOGGER "INFO" "Today batch date is ${V_DATA_DATE}."


# 固定参数
echo "#V_FREQUENCY#=${V_FREQUENCY}=任务类型"                                                   > "${V_PARM_FILE}"
echo "#V_DATA_DATE#=${V_DATA_DATE}=数据日期"                                                   >> "${V_PARM_FILE}"
if [[ ${V_DATA_DATE:4:4} -eq 1331 ]];then
  echo "#V_CYC_DATE#=${V_DATA_DATE:0:4}1231=上月"                                            >> "${V_PARM_FILE}"
  echo "#V_CYC_12M_DATE#=$((${V_DATA_DATE:0:4}-1))1331=去年同期"                             >> "${V_PARM_FILE}"
  echo "#V_CURR_DATE#=$((${V_DATA_DATE:0:4}+1))0131=本月末"                                                 >> "${V_PARM_FILE}"
  echo "#V_LAST_1M_END_DATE#=${V_DATA_DATE:0:4}1231=上月末"                                  >> "${V_PARM_FILE}"
  echo "#V_LAST_12M_END_DATE#=$((${V_DATA_DATE:0:4}-1))1331=月末去年同期"                    >> "${V_PARM_FILE}"
  # 自定义参数
  echo "#V_LINE_DATE#=${V_DATA_DATE:0:4}-${V_DATA_DATE:4:2}-${V_DATA_DATE:6:2}=数据日期_带横线"  >> "${V_PARM_FILE}"
  echo "#V_YEAR_FIRST_DAY#=$((${V_DATA_DATE:0:4}-1))1331=年初第一天"                                    >> "${V_PARM_FILE}"
  echo "#V_LAST_1Y_FIRST_DAY#=$((${V_DATA_DATE:0:4}-2))1331=去年第一天"                          >> "${V_PARM_FILE}"
  echo "#V_LAST_1Y_END_DAY#=$((${V_DATA_DATE:0:4}-1))1331=上年末"                                >> "${V_PARM_FILE}"
  ## 8位月份计算
  echo "#V_LAST_2M_END_DATE#=${V_DATA_DATE:0:4}1130=上2月末"                             >> "${V_PARM_FILE}"
  echo "#V_LAST_13M_END_DATE#=$((${V_DATA_DATE:0:4}-1))1231=上13月末"                         >> "${V_PARM_FILE}"
  echo "#V_LAST_17M_END_DATE#=$((${V_DATA_DATE:0:4}-1))0831=上17月末"                         >> "${V_PARM_FILE}"
  ## 6位月份计算
  echo "#V_DATA_MONTH#=${V_DATA_DATE:0:6}=数据月份"                                              >> "${V_PARM_FILE}"
  echo "#V_LAST_1M_END_MONTH#=${V_DATA_DATE:0:4}12=上月"                                         >> "${V_PARM_FILE}"
  echo "#V_LAST_12M_END_MONTH#=$((${V_DATA_DATE:0:4}-1))13=上12月"                               >> "${V_PARM_FILE}"
  DT=$(F_END_DATE ${V_DATA_DATE} -13)
  echo "#V_LAST_13M_END_MONTH#=$((${V_DATA_DATE:0:4}-1))12=上13月"                               >> "${V_PARM_FILE}"
  ## 带横杠的日期格式
  echo "#V_MONTH_FIRST_DATE_LINE#=${V_DATA_DATE:0:4}-13-31=月初"                                 >> "${V_PARM_FILE}"
else
  echo "#V_CYC_DATE#=$(F_CYC_DATE ${V_DATA_DATE} -1)=上月"                                       >> "${V_PARM_FILE}"
  echo "#V_CYC_12M_DATE#=$(F_CYC_DATE ${V_DATA_DATE} -12)=去年同期"                              >> "${V_PARM_FILE}"
  echo "#V_CURR_DATE#=$(F_END_DATE ${V_DATA_DATE} -0)=本月末"                                    >> "${V_PARM_FILE}"
  echo "#V_LAST_1M_END_DATE#=$(F_END_DATE ${V_DATA_DATE} -1)=上月末"                             >> "${V_PARM_FILE}"
  echo "#V_LAST_12M_END_DATE#=$(F_END_DATE ${V_DATA_DATE} -12)=月末去年同期"                     >> "${V_PARM_FILE}"
  # 自定义参数
  echo "#V_LINE_DATE#=${V_DATA_DATE:0:4}-${V_DATA_DATE:4:2}-${V_DATA_DATE:6:2}=数据日期_带横线"  >> "${V_PARM_FILE}"
  echo "#V_YEAR_FIRST_DAY#=$((${V_DATA_DATE:0:4}-1))1331=年初第一天"                                    >> "${V_PARM_FILE}"
  echo "#V_LAST_1Y_FIRST_DAY#=$((${V_DATA_DATE:0:4}-2))1331=去年第一天"                          >> "${V_PARM_FILE}"
  echo "#V_LAST_1Y_END_DAY#=$((${V_DATA_DATE:0:4}-1))1331=上年末"                                >> "${V_PARM_FILE}"
  ## 8位月份计算
  echo "#V_LAST_2M_END_DATE#=$(F_END_DATE ${V_DATA_DATE} -2)=上2月末"                             >> "${V_PARM_FILE}"
  echo "#V_LAST_13M_END_DATE#=$(F_END_DATE ${V_DATA_DATE} -13)=上13月末"                         >> "${V_PARM_FILE}"
  echo "#V_LAST_17M_END_DATE#=$(F_END_DATE ${V_DATA_DATE} -17)=上17月末"                         >> "${V_PARM_FILE}"
  ## 6位月份计算
  echo "#V_DATA_MONTH#=${V_DATA_DATE:0:6}=数据月份"                                              >> "${V_PARM_FILE}"
  DT=$(F_END_DATE ${V_DATA_DATE} -1)
  echo "#V_LAST_1M_END_MONTH#=${DT:0:6}=上月"                                                  >> "${V_PARM_FILE}"
  DT=$(F_END_DATE ${V_DATA_DATE} -12)
  echo "#V_LAST_12M_END_MONTH#=${DT:0:6}=上12月"                                               >> "${V_PARM_FILE}"
  DT=$(F_END_DATE ${V_DATA_DATE} -13)
  echo "#V_LAST_13M_END_MONTH#=${DT:0:6}=上13月"                                               >> "${V_PARM_FILE}"
  ## 带横杠的日期格式
  echo "#V_MONTH_FIRST_DATE_LINE#=${V_DATA_DATE:0:4}-${V_DATA_DATE:4:2}-01=月初"                  >> "${V_PARM_FILE}"
fi
LOGGER "INFO" "Success to create parm file[${V_PARM_FILE}]."
END
exit 0
